package edu.zzu.daoImpl;

import edu.zzu.Entity.DLS;
import edu.zzu.Entity.Delivery;
import edu.zzu.Entity.Package;
import edu.zzu.Entity.PackageRoute;
import edu.zzu.dao.PackageDao;
import edu.zzu.mapper.PackageMapper;
import edu.zzu.mapper.PackageRouteMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Date;
import java.sql.Time;
import java.util.List;

@Component
public class PackageDaoImpl implements PackageDao {

    @Autowired
    private PackageMapper packageMapper;

    @Autowired
    private PackageRouteMapper packageRouteMapper;

    @Override
    public List<Package> getPackageBySenderTel(String tel) {
        return packageMapper.selectPackageBySenderTel(tel);
    }

    @Override
    public List<Package> getPackageByReceiverTel(String tel) {
        return packageMapper.selectPackageByReceiverTel(tel);
    }

    @Override
    public int savePackage(Package p) {
        return packageMapper.insert(p);
    }

    @Override
    public boolean updatePackageStatus(String id, Integer status,Long handler,Long transNodeId,Float x,Float y) {
        String ctime=new Date(System.currentTimeMillis()) + " "+ new Time(System.currentTimeMillis());

        packageRouteMapper.insert(new PackageRoute(null,id,ctime,transNodeId,handler,status,x,y));
        return packageMapper.updatePackageStatus(id, status,handler,ctime,transNodeId,x,y) == 1;
    }

    @Override
    public List<DLS> selectDLSByRegionCode(String regionCode) {
        return packageMapper.selectDLSByRegionCode(regionCode);
    }

    @Override
    public List<Delivery> selectDeliveryByHandler(Long handler) {
        return packageMapper.selectDeliveryByHandler(handler);
    }

    @Override
    public Package selectPackageById(String id) {
        return packageMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean checkPackageExistById(String id) {
        return packageMapper.existsWithPrimaryKey(id);
    }

    @Override
    public Long selectPackageAmountByTransNodeId(Long id) {
        return packageMapper.selectPackageAmountByTransNodeId(id);
    }

    @Override
    public List<Package> selectPackagesOfTransNodeId(Long id) {
        return packageMapper.selectPackagesOfTransNode(id);
    }

    @Override
    public List<Package> selectPackagesByStatus(Integer status) {
        return packageMapper.selectPackagesByStatus(status);
    }
}
